﻿/*
* ==========================================================
* 说明：
* ==========================================================
*/
(function () {
    appModule.controller('tenant.views.repairTask.index', [
        '$scope', '$uibModal', 'abp.services.app.test',
        function ($scope, $uibModal, testService) {
            //测试数据源
            var typesSelectDataSource = [
                { name: '选项一', value: 1 },
                { name: '选项二', value: 2 },
                { name: '选项三', value: 3 },
            ]

            /*------------------------------------------------------------------------
            * 参数
            *--------------------------------------------------------------------------
            */
                $scope.conf = [];
                var AppUrl = '/api/services/app/';//服务地址
                var vm = this;
                vm.filters = [];//列表初始过滤条件
                vm.filtersAreShown = false;//是否显示搜索功能
                vm.timeOperatorDataSource = [//时间对比参数
                    { name: '等于', code: 'eq' },
                    { name: '不等于', code: 'neq' },
                    { name: '大于', code: 'gt' },
                    { name: '大于等于', code: 'gte' },
                    { name: '小于', code: 'lt' },
                    { name: '小于等于', code: 'lte' },
                ]
                vm.curTest = null;//当前选中的列的数据

            /**
             * 权限配置函数：vm.permissions
            **/
            vm.permissions = {
                "create": abp.auth.hasPermission('Pages.Tenant.Test.Create'),//添加
                "update": abp.auth.hasPermission('Pages.Tenant.Test.Update'),//修改
                "delete": abp.auth.hasPermission('Pages.Tenant.Test.Delete'),//删除
                "searchAndReset": true,//搜索&重置
            };

            /**
             * 搜索功能配置函数：vm.searchConditionsData
             *
             * @param {boole} state 状态,当前配置对象是否启用
             * @param {string} name 名称
             * @param {string} code 标识
             * @param {string} type 类型
             * type(value):
             *  'input_text' => 文本输入框
             *  'input_time' => 时间输入框
             *  'select' => 下拉框
             * @param {object or array} conf 自定义参数配置
             * @param {object} searchFilters 搜索过滤器
             * @param {string} searchFilters.logic 逻辑范围
             * logic(value):
             *  'and' => 与
             *  'or' => 或
             * @param {string} searchFilters.operator 对比参数
             * operator(value):
             *  'eq' => 等于
             *  'neq' => 不等于
             *  'gt' => 大于
             *  'gte' => 大于等于
             *  'lt' => 小于
             *  'lte' => 小于等于
             *  'contains' => 包含
             *  'doesnotcontain' => 不包含
             *  'startswith' => 开头为
             *  'endswith' => 结尾为
             *
             * ===示例===
               {
                   state: true,
                   name: '使用者姓名',
                   code: 'userName',
                   type: 'input_text',
                   conf: null,
                   searchFilters: {
                       logic: 'and',
                       operator: 'contains',
                   },
               },
               {
                   state: true,
                   name: '下拉框',
                   code: 'key',
                   type: 'select',
                   conf: {
                       dataSource: typesSelectDataSource,
                       optionLabel: "请选择",
                       dataTextField: "name",//显示字段
                       dataValueField: "value",//值
                   },
                   searchFilters: {
                       logic: 'and',
                       operator: 'eq',
                   },
               },
               {
                   state: true,
                   name: '注册时间',
                   code: 'registerTime',
                   type: 'input_time',
                   conf: [
                       {
                           dataSource: vm.timeOperatorDataSource,
                           optionLabel: "请选择条件",
                           dataTextField: "name",//显示字段
                           dataValueField: "code",//值
                       },
                       {
                           format: 'yyyy-MM-dd HH:mm',
                           value: null,//string：2017-9-9 15:50,
                       }
                   ],
                   searchFilters: {
                       logic: 'and',
                       operator: null,
                   },
               },
            **/
            vm.searchConditionsData = [
               {
                   state: true,
                   name: '状态id',
                   code: 'stateID',
                   type: 'input_text',
                   conf: null,
                   searchFilters: {
                       logic: 'and',
                       operator: 'eq',
                   },
               },
               {
                   state: true,
                   name: 'key值',
                   code: 'key',
                   type: 'input_text',
                   conf: null,
                   searchFilters: {
                       logic: 'and',
                       operator: 'contains',
                   },
               },
               {
                   state: true,
                   name: 'value值类型',
                   code: 'value',
                   type: 'select',
                   conf: {
                       dataSource: typesSelectDataSource,
                       optionLabel: "请选择",
                       dataTextField: "name",//显示字段
                       dataValueField: "name",//值
                   },
                   searchFilters: {
                       logic: 'and',
                       operator: 'eq',
                   },
               },
               {
                   state: true,
                   name: '测试时间',
                   code: 'testTime',
                   type: 'input_time',
                   conf: [
                       {
                           dataSource: vm.timeOperatorDataSource,
                           optionLabel: "请选择条件",
                           dataTextField: "name",//显示字段
                           dataValueField: "code",//值
                       },
                       {
                           format: 'yyyy-MM-dd HH:mm',
                           value: null,//string：2017-9-9 15:50,
                       }
                   ],
                   searchFilters: {
                       logic: 'and',
                       operator: null,
                   },
               },
            ];

            /*
            * kendoUI控件
            */
            //表格
            var mainGridDataSource = new kendo.data.DataSource({//列表数据源
                type: 'aspnetmvc-ajax',
                transport: {
                    read: {
                        url: AppUrl + 'test/KendoGetTests',
                        contentType: 'application/json',
                        type: 'POST'
                    },
                    parameterMap: function (options, operation) {
                        return JSON.stringify(options);
                    }
                },
                pageSize: 10,
                serverPaging: true,
                serverFiltering: true,
                serverSorting: true,
                schema: {
                    data: function (data) {
                        if (data.success === true && data.result !== null) {
                            vm.curTest = null;
                            console.log('data.result.data:', data.result.data);
                            return data.result.data;
                        }
                        return [];
                    },
                    total: function (data) {
                        if (data.success === true && data.result !== null) {
                            return data.result.total;
                        }
                        return 0;
                    },
                    model: {
                        id: 'id',
                        fields: {
                            id: { type: 'number' },
                            creationTime: { type: 'date' },
                            testTime: { type: 'date' },
                        }
                    }
                },
               filter: {
                    "logic": "and",
                    "filters": vm.filters
                }
            });
            vm.mainGridOptions = {//列表配置
                dataSource: mainGridDataSource,
                change: function (e) {
                    var selectedRows = this.select();
                    for (var i = 0; i < selectedRows.length; i++) {
                        vm.curTest = this.dataItem(selectedRows[i]);
                    }
                },
                columnMenu: true,
                allowCopy: true,
                selectable: 'multiple',
                sortable: {
                   mode: 'multiple'
                },
                pageable: {
                    buttonCount: 5.0,
                    pageSize: 10,
                    pageSizes: [10, 20, 50, 100],
                    refresh: true
                },
                columns: [
                    {
                        //locked: true,
                        title: '操作',
                        width: 110,
                        template: '<button class="btn btn-primary" ng-click="vm.openInfoTestModal(dataItem)"><i class="icon-magnifier"></i><span>查看详情</span></button>',
                    }, 
                    { 
                        title: '状态id',
                        field: 'stateID',
                        width: 100,
                    },
                    {
                        title: 'key值',
                        field: 'key',
                        width: 100,
                    },
                    {
                        title: 'value值',
                        field: 'value',
                        width: 100,
                    },
                    { 
                        title: '测试时间',
                        field: 'testTime',
                        width: 100,
                        format: "{0:yyyy-MM-dd HH:mm}",
                    },
                    {
                        title: '创建时间',
                        field: 'creationTime',
                        width: 100,
                        format: "{0:yyyy-MM-dd HH:mm}",
                    },
                ]
            };
            //时间
            vm.TimeOptions = function (conf) {
                var thisTimeValue = conf.defaultTime;
                if (thisTimeValue) {
                    thisTimeValue = new Date(thisTimeValue);
                } else {
                    thisTimeValue = new Date();
                }
                return {
                    format: conf.format,
                    value: thisTimeValue,
                }
            };
            //下拉框
            vm.SelectOptions = function (conf) {
                return {
                    dataSource: conf.dataSource,
                    optionLabel: conf.optionLabel,
                    dataTextField: conf.dataTextField,
                    dataValueField: conf.dataValueField,
                }
            }

            /*---------------------------------------------------------------------------------------------------------------
            * 可共用的function
            */
            //添加&修改
            //function openCreateOrUpdateTestModal(testId) {
            //    var modalInstance = $uibModal.open({
            //        templateUrl: '~/App/tenant/views/test/createOrEditTest.cshtml',
            //        controller: 'tenant.views.test.createOrEditTest as vm',
            //        backdrop: 'static',
            //        size: 'more-lg',
            //        resolve: {
            //            parameters: {
            //                testId: testId,
            //            }
            //        }
            //    });

            //    modalInstance.result.then(function (result) {
            //        vm.mainGridOptions.dataSource.read();
            //    });
            //};



            /*---------------------------------------------------------------------------------------------------------------
            * 所有方法
            */
            //添加
            //vm.CreateTest = function () {
            //    openCreateOrUpdateTestModal(null);
            //};
            //修改
            //vm.UpdateTest = function () {
            //    if (vm.curTest) {
            //        openCreateOrUpdateTestModal(vm.curTest.id);
            //    } else {
            //        abp.message.error('请选择要修改的行！');
            //    }
            //};
            //删除
            vm.DeleteTest = function () {
                if (vm.curTest) {
                    abp.message.confirm(
                        '是否要删除此条记录！',
                        function (isConfirmed) {
                            if (isConfirmed) {
                                testService.deleteTest({
                                    id: vm.curTest.id
                                }).success(function (result) {
                                    abp.notify.success('删除成功！');
                                }).finally(function () {
                                    vm.mainGridOptions.dataSource.read();
                                });
                            }
                        }
                   );
                } else {
                    abp.message.error('请选择要删除的行！');
                }
            };
            //查看详情
            vm.openInfoTestModal = function (dataItem) {
                var modalInstance = $uibModal.open({
                    templateUrl: '~/App/tenant/views/test/createOrEditTest.cshtml',
                    controller: 'tenant.views.test.createOrEditTest as vm',
                    backdrop: 'static',
                    size: 'more-lg',
                    resolve: {
                        parameters: {
                            rowData: dataItem,
                            testId: dataItem.id
                        }
                    }
                });

                modalInstance.result.then(function (result) {

                });
            };
            //搜索
            vm.Search = function () {
                console.log($scope.conf);
                var SearchFilters = [];
                for (var i = 0; i < vm.searchConditionsData.length; i++) {
                    if (vm.searchConditionsData[i].state && $scope.conf[i]) {
                        var operator = vm.searchConditionsData[i].searchFilters.operator;
                        var value = $scope.conf[i];
                        if (vm.searchConditionsData[i].type == 'input_time') {
                            operator = $scope.conf[i].operatorValue;
                            value = new Date($scope.conf[i].timeValue);
                            if (!operator) {
                                break;
                                return false;
                            }
                        }
                        SearchFilters.push({
                            logic: vm.searchConditionsData[i].searchFilters.logic,
                            filters: [
                                {
                                    field: vm.searchConditionsData[i].code,
                                    operator: operator,
                                    value: value,
                                },
                            ]
                        });
                    }
                }
                vm.mainGridOptions.dataSource.filter({
                    logic: 'and',
                    filters: vm.filters.concat(SearchFilters)
                });
                abp.notify.success('搜索完成！');
            };
            //重置
            vm.Reset = function () {
                vm.filtersAreShown = false;
                $scope.conf = [];
                vm.mainGridOptions.dataSource.filter({
                    logic: 'and',
                    filters: vm.filters
                });
                abp.notify.success('重置完成！');
            };
        }
    ]);
})();






